package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "03.微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    /**
     * 1.发布微博功能
     */
    @ApiOperation(value = "发布微博功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        /*
            1.接收客户端传递的数据;
            2.校验用户登录状态:session.getAttribute("user");
              2.1 未登录状态: return 2;
              2.2 登录状态: 微博表中插入数据[INSERT接口], return 1;
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO = " + userVO);
        // 未登录,发布失败
        if (userVO == null) {
            log.debug("用户未登录");
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        // 登录状态,发布成功
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());

        weiboMapper.insert(weibo);
        log.debug("插入" + weibo + "成功");
        return JsonResult.ok();
    }

    /**
     * 2.微博首页列表
     */
    @ApiOperation(value = "微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex() {
        log.debug("微博首页查询成功");
        List<WeiboIndexVO> weiboIndexVOList = weiboMapper.selectIndex();
        return JsonResult.ok(weiboIndexVOList);
    }

    /**
     * 3.微博详情功能
     */
    @ApiOperation(value = "微博详情功能")
    @GetMapping("selectById")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "微博ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    })
    public JsonResult selectById(@Range(min = 1, max = 999, message = "请提交合法的ID值") int id, String username) {
        if (id < 0) {
            throw new IllegalArgumentException("微博id值无效");
        }
        log.debug("查看微博ID：" + id + "成功");
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}









